require('shelljs/global')

try {
    hexo.on('deployAfter', function() {
    	//当deploy完成后执行备份
        runBackup();
    });
} catch (e) {
    console.log("自去备份源码出现错误!，错误详情：" + e.toString());
}

function runBackup() {
    if (!which('git')) {
        echo('你需要安装Git.');
        exit(1);
    }
    echo(">>>>>>>>>>>>>>>>> 自动备份博客源码开始 <<<<<<<<<<<<<<<<<");

    cd("E:\\workspaces\\blog");

    if (exec('git add --all').code !== 0) {
        echo('Error: Git add failed');
        exit(1);
    }

    if (exec('git commit -am "From auto backup script\'s commit"').code !== 0) {
        echo('Error: Git commit failed');
        exit(1);
    }

    if (exec('git push origin master').code !== 0) {
        echo('Error: Git push failed');
        exit(1);
    }

    echo(">>>>>>>>>>>>>>>>> 自动备份博客源码结束 <<<<<<<<<<<<<<<<<");

}
